package com.wcl.nullmusic.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseActivity;
import com.wcl.nullmusic.base.BaseAppcation;
import com.wcl.nullmusic.mode.SongDetailResult;
import com.wcl.nullmusic.mode.SongDetail_Info;
import com.wcl.nullmusic.mode.SongmenuResult;
import com.wcl.nullmusic.ui.adapter.SongDetailRecyclerViewAdapter;
import com.wcl.nullmusic.utils.Contant;
import com.wcl.nullmusic.utils.ToActivityUtils;
import com.wcl.nullmusic.utils.VolleyUtils;
import com.wcl.nullmusic.view.Bookends;
import com.wcl.nullmusic.view.CircleTransformation;

/**
 * Created by dllo on 16/1/17.
 */
public class SongDetailActivity extends BaseActivity implements View.OnClickListener, Toolbar.OnMenuItemClickListener {
    private RecyclerView recyclerView;
   // private RecyclerViewHeader singdetail_recyclerViewHeader;
    private Toolbar singdetail_toolBar;
    private String specialId;
    private SongDetailRecyclerViewAdapter adapter;
    private TextView singdetail_title, singdetail_desc, singdetail_author;
    private ImageView singdetail_authorPic;
    private Bookends<SongDetailRecyclerViewAdapter> bookends;


    @Override
    protected void conService() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_songdetail;
    }

    @Override
    protected void getVaule() {
        Bundle bundle = ToActivityUtils.fromSrc(this);
        if (bundle != null) {
            this.specialId = bundle.getString("specialId");
        }

    }

    @Override
    protected void initView() {
        BaseAppcation.getInstance().addActivity(this);
        recyclerView = findView(R.id.singdetail_recyclerView);
      //  singdetail_recyclerViewHeader = findView(R.id.singdetail_recyclerViewHeader);
        singdetail_toolBar = findView(R.id.singdetail_toolBar);
        adapter = new SongDetailRecyclerViewAdapter(this);
        bookends=new Bookends<>(adapter);
//        singdetail_title = findView(R.id.singdetail_title);
//        singdetail_desc = findView(R.id.singdetail_desc);
//        singdetail_author = findView(R.id.singdetail_author);
//        singdetail_authorPic = findView(R.id.singdetail_authorPic);
    }

    @Override
    protected void initData() {
        singdetail_toolBar.setTitle("听单详情");
        singdetail_toolBar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(singdetail_toolBar);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bookends.addHeader(getHeaderView());
        recyclerView.setAdapter(bookends);
     //   recyclerView.setAdapter(adapter);
     //   singdetail_recyclerViewHeader.attachTo(recyclerView, true);
        singdetail_toolBar.setNavigationIcon(R.mipmap.mk_ic_web_back);

        loadData();
    }

    private View getHeaderView(){
        View view= LayoutInflater.from(this).inflate(R.layout.songddetail_recycler_header,recyclerView,false);
        singdetail_title = (TextView) view.findViewById(R.id.singdetail_title);
        singdetail_desc = (TextView) view.findViewById(R.id.singdetail_desc);
        singdetail_author = (TextView) view.findViewById(R.id.singdetail_author);
        singdetail_authorPic = (ImageView) view.findViewById(R.id.singdetail_authorPic);
        return view;
    }

    @Override
    protected void listener() {
        singdetail_toolBar.setNavigationOnClickListener(this);
        singdetail_toolBar.setOnMenuItemClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.songdetail_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void loadData() {
        VolleyUtils.getInstance().StringRequest(Contant.RECOMMEND_VIEWPAGER_TYPE9_URL + specialId, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongDetailResult result = gson.fromJson(response, SongDetailResult.class);
                SongDetail_Info info = result.getInfo();
                singdetail_title.setText(info.getTitle());
                singdetail_desc.setText(info.getIntro());
                singdetail_author.setText(info.getNickname());
                Picasso.with(SongDetailActivity.this).load(info.getSmallLogo()).placeholder(R.mipmap.ic_classify_img06)
                        .error(R.mipmap.ic_classify_img06).transform(new CircleTransformation()).into(singdetail_authorPic);

                adapter.addData(result.getList());

            }
        }, "SongDetail_RecyclerView_StringRequest");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case 0:
                break;
            default:
                finish();
                break;
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.songdetail_menu_share:
                Toast.makeText(SongDetailActivity.this, "分享", Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        BaseAppcation.getInstance().finishActivity(this);
        super.onDestroy();
    }
}
